//
//  LYCMineViewController.m
//  爱鲜蜂
//
//  Created by 廖易诚 on 16/6/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "LYCMineViewController.h"
#import "UMSocial.h"

@interface LYCMineViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

///
@property (nonatomic, weak) UITableView *mineView;
///
@property (nonatomic, weak) UIImageView *iconImageView;

@end

@implementation LYCMineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark - setupUI
- (void)setupUI {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UITableView *mineView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:mineView];
    self.mineView = mineView;
    mineView.delegate = self;
    mineView.dataSource = self;
    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 200)];
    mineView.tableHeaderView = headerView;
    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
    
    UIImageView *iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
    self.iconImageView = iconImageView;
    iconImageView.userInteractionEnabled = YES;
    
    [headerView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerView);
        make.top.mas_equalTo(30);
        make.width.height.mas_equalTo(80);
    }];
    iconImageView.layer.cornerRadius = 40;
    iconImageView.layer.masksToBounds = YES;
    
    UIButton *userIconBtn = [[UIButton alloc] init];
    [iconImageView addSubview:userIconBtn];
    userIconBtn.frame = iconImageView.bounds;
    userIconBtn.backgroundColor = [UIColor clearColor];
    [userIconBtn addTarget:self action:@selector(selectUerIcon) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    [headerView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iconImageView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(iconImageView);
    }];
    phoneLabel.text = @"13552892120";
}

#pragma mark - 选择用户头像
- (void)selectUerIcon {
    /// 打开相机或者选择相册中图片

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    // 判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
            pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
            pickerVC.allowsEditing = YES;
            pickerVC.delegate = self;
            [self presentViewController:pickerVC animated:YES completion:nil];
        }]];
        
        
    }
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pickerVC.allowsEditing = YES;
        pickerVC.delegate = self;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

#pragma mark - 实现pickerViewController的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [self dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    // dealWithImage
    image = [self dealWithImage:image];
    
    self.iconImageView.image = image;
}

#pragma mark - 处理图片
- (UIImage *)dealWithImage:(UIImage *)image {
    
//    UIGraphicsBeginImageContextWithOptions(self.iconImageView.bounds.size, 0, 0);
//    
//    [image drawInRect:self.iconImageView.bounds];
//    
//    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
//    
//    UIGraphicsEndImageContext();
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    
    return [UIImage imageWithData:imageData];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

/// 设置多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else if(section == 1) {
        return 1;
    }else {
        return 2;
    }
}

// 显示每一个cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"我的收获地址";
            cell.imageView.image = [UIImage imageNamed:@"v2_my_address_icon"];
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"我的店铺";
            cell.imageView.image = [UIImage imageNamed:@"icon_mystore"];
        }
    }else if(indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"把我的爱鲜蜂分享给朋友";
            cell.imageView.image = [UIImage imageNamed:@"v2_my_share_icon"];
        }
    }else if(indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"客服帮助";
            cell.imageView.image = [UIImage imageNamed:@"v2_my_serviceonline_icon"];
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"意见反馈";
            cell.imageView.image = [UIImage imageNamed:@"feedBack_service"];
        }
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        [self alertShareTypeView];
    }
}

#pragma mark - 弹出分享提示框
- (void)alertShareTypeView {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"分享到" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
        [alertVC addAction:[UIAlertAction actionWithTitle:@"微信朋友圈" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"微信好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"QQ空间" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //需要自定义面板样式的开发者需要自己绘制UI，在对应的分享按钮中调用此接口
        [UMSocialData defaultData].extConfig.title = @"分享的title";
        [UMSocialData defaultData].extConfig.qqData.url = @"http://baidu.com";
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:
                                            @"http://www.baidu.com/img/bdlogo.gif"];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQzone] content:@"分享内嵌文字" image:nil location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];

    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
